package de.koller.worlders.engine;

import de.koller.oceanFramework.gl.util.InputGL;
import de.koller.oceanFramework.tree.Child;
import de.koller.worlders.WSea;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WInputGL extends WSea {
	
	@Child private final InputGL input = new InputGL();
	
	private boolean mouseGrab = false;
	private Object exclusive = null;
	private Object reference = null;
	
	public void setMouseGrab( boolean grab ) {
		if( mouseGrab == grab )
			return;
		
		mouseGrab = grab;
		
		if( mouseGrab )
			input.grabMouse();
		else
			input.unGrabMouse();
	}
	
	public void setExclusivity( Object exclusive ) {
		this.exclusive = exclusive;
	}
	
	public void unsetExclusivity() {
		this.exclusive = null;
	}
	
	public void useReference( Object reference ) {
		this.reference = reference;
	}
	
	public void unuseReference() {
		this.reference = null;
	}
	
	private boolean isAllowedInput() {
		if( exclusive == null )
			return true;
		else
			return exclusive.equals( reference );
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
//		openBrowser();
	}
	
//	private void openBrowser() {
//		if( !this.isKeyPressed( Keyboard.KEY_F1 ) )
//			return;
//		
//		if( !Desktop.isDesktopSupported() ) {
//			System.err.println( "Desktop is not supported" );
//			return;
//		}
//		
//		Desktop desktop = Desktop.getDesktop();
//		
//		if( !desktop.isSupported( Desktop.Action.BROWSE ) ) {
//			System.err.println( "Can't open website" );
//			return;
//		}
//		
//		try {
//			URI uri = new URI( "www.koller-digital.de" );
//			desktop.browse( uri );
//		} catch ( Exception ex ) {
//			System.err.println( "Can't open website: " + ex.toString() );
//		}
//		
//	}

	public boolean isMouseReleased( int button ) {
		if( !this.isAllowedInput() ) return false;
		return input.isMouseReleased( button );
	}

	public boolean isMousePressed( int button ) {
		if( !this.isAllowedInput() ) return false;
		return input.isMousePressed( button );
	}

	public boolean isMouseDown( int mouseButton ) {
		if( !this.isAllowedInput() ) return false;
		return input.isMouseDown( mouseButton );
	}

	public boolean isKeyReleased( int key ) {
		if( !this.isAllowedInput() ) return false;
		return input.isKeyReleased( key );
	}

	public boolean isKeyPressed( int key ) {
		if( !this.isAllowedInput() ) return false;
		return input.isKeyPressed( key );
	}

	public boolean isKeyDown( int key ) {
		if( !this.isAllowedInput() ) return false;
		return input.isKeyDown( key );
	}

	public Set<Integer> getPressedKeys() {
		if( !this.isAllowedInput() ) return new HashSet<>();
		return input.getPressedKeys();
	}

	public int getMouseY() {
		if( !this.isAllowedInput() ) return -1;
		return input.getMouseY();
	}

	public int getMouseX() {
		if( !this.isAllowedInput() ) return -1;
		return input.getMouseX();
	}

	public float getMouseScaledY() {
		if( !this.isAllowedInput() ) return -1;
		return input.getMouseScaledY();
	}

	public float getMouseScaledX() {
		if( !this.isAllowedInput() ) return -1;
		return input.getMouseScaledX();
	}

	public int getMouseDY() {
		if( !this.isAllowedInput() ) return 0;
		return input.getMouseDY();
	}

	public int getMouseDX() {
		if( !this.isAllowedInput() ) return 0;
		return input.getMouseDX();
	}

	public int getDMouseWheel() {
		if( !this.isAllowedInput() ) return 0;
		return input.getDMouseWheel();
	}

	public List<Character> getCharactersPressed() {
		if( !this.isAllowedInput() ) return new ArrayList<>();
		return input.getCharactersPressed();
	}

}
